/*
 * @Description: 加签的一些方法
 */
import { useBaseInfoWithOut } from '@/store/modules/base-info';
import { getQueryString } from '@/plugins/methods';
import api from '@/api';
import emitter from "@/plugins/global-callback";

const useBaseInfo = useBaseInfoWithOut();
const appName = 'collaboration';
//是否是处理页面
const isSummary = getQueryString('method') === 'summary';

/**
 * @description: 加签给某些人。
 * @param {Object} params 加签参数
 * @param {String|number} params 人员ID
 * @return {*}
 */
export async function addNodeMessage(params, id) {
  //获取人员信息
  const { processActionDetail, processAction } = await api.summaryGetSignPeopleInfo(id);
  params.needAddNodeDiagram = 'false';
  //传进去
  Object.assign(processActionDetail, params.changeNodeMessage || {});
  params.changeNodeMessage = processActionDetail;
  params.changeNodeMessage.processAction = processAction;
}

//加签后的回调
export function insertNodeCallBack(param) {
  console.log('insertNodeCallBack', param);
  if (!isSummary) {
    //【V5-17834】加签后，新建页面的流程框没有显示出加签节点的信息
    emitter.publish("insertNode", param);
    useBaseInfo.$patch((state) => {
      state.formInfo.workflowInfo.workflowNodesInfo.value = param.workflowNodeInfo;
      state.hasAddNode = true;
    });
    //--------------------
    const {
      workflow: { processId, processTemplateId },
      currentUser: { name, loginAccountName },
      affair: { appEnumStr },
      other: { moduleTypeName },
    } = useBaseInfo.baseInfo.businessData;
    showDiagram({
      targetWin: window.top,
      valueWin: window,
      isTemplate: true,
      isDebugger: false,
      buttons: [{
        "id": "ok"
      }],
      scene: 1,
      SPK: '0',
      isModalDialog: true,
      processId: processId || processTemplateId,
      currentAccountName: loginAccountName,
      appName: appEnumStr || moduleTypeName,
      currentUserName: name,
      // callBackFn: resolve,
    });
  }
}

/**
 * @description: 生成加签/当前会签/知会/多级会签 流程的时候需要设置流程的基础参数
 * @param {String} type 区分是加签addNode/当前会签jointSign/知会inform/多级会签moreSign
 * @return {Object}最终参数
 */
export function getBasicParams(type) {
  const {
    template,
    summary: { id: summaryId },
    affair: { activityId: nodeId, formRecordid: formRecordId, memberId },
    currentUser: { adminType, id: currentUserId, name: currentUserName, loginAccount: accountId },
    workflow: {
      itemId: workitemId,
      processId,
      caseId,
      affairId,
      // senderId,貌似跟memberId一样
      processTemplateId,
    },
    nodePolicy: { flowPermAccountId, nodePolicy, nodePolicyName },
  } = useBaseInfo.baseInfo.businessData;
  const { secretLevel } = useBaseInfo.baseInfo;

  const isForm = template?.bodyType === '20';
  //处理页面
  if (isSummary) {
    //在原逻辑基础上改进了下写法
    const params = {
      workitemId,
      processId: processId || processTemplateId,
      nodeId,
      caseId,
      appName: 'collaboration',
      isForm,
      affairId,
      useNowexpirationTime: 'true',
      NPS: isForm ? 'form' : 'default',
      accountId: flowPermAccountId,
      flowPermAccountId,
      isNeedCheckLevelScope: true,
      accessFileSecretLevel: secretLevel?.value || '',
      changeNodeMessage: {},
    };

    const spkObj = {
      addNode: 'insertNode',
      inform: 'informNode',
      jointSign: 'assignNode',
      moreSign: 'nodeOperation4Account',
    };

    let SPK = spkObj[type];
    if (type === 'moreSign') {
      if (adminType === 'formAdmin') {
        SPK = 'nodeOperation4Form';
      }
      Object.assign(params, {
        affairMemberId: currentUserId,
        workitemId,
        selectPeopleDialogLeftTitle: '',
        selectType: 'Department,Member,BusinessDepartment,Post',
        panels: 'Department,Team,BusinessDepartment,Post',
      });
    } else {
      Object.assign(params, {
        performer: currentUserId,
        summaryId,
        formRecordId,
      });
    }

    params.SPK = SPK;

    if (type === 'inform') {
      params.defaultPolicyId = 'collaboration';
    } else if (type === 'jointSign') {
      params.defaultPolicyId = nodePolicy;
      params.defaultPolicyName = nodePolicyName;
    }

    if (type === 'addNode' || type === 'moreSign') {
      params.isTemplete = template?.isTemplate;
    }
    return params;
  } else {
    //新建
    return {
      workitemId: '',
      processId: processId || processTemplateId,
      nodeId: 'start',
      currentUserId,
      performer: currentUserId,
      caseId: '',
      appName,
      isForm,
      accountId,
      callback: insertNodeCallBack,
      callBackFn: insertNodeCallBack,//多级会签回调是这个
      summaryId,
      affairId,
      isTemplete: template?.isTemplate,
      useNowexpirationTime: 'true',
      SPK: 'default',
      formRecordId,
      needValidateOnBeforeAdd: 'false',
      needAddNodeDiagram: 'true',
      changeNodeMessage: {
        NPS: 'default',
        appName,
        currentUserId,
        currentUserName,
      },
      accessFileSecretLevel: secretLevel?.value || '',
      NPS: template?.bodyType == '20' ? 'form' : 'default',
    };
  }
}

/**
 * @description: 获取加签传参
 * @param {String|Number|Object} people 人员数据\ID 用于自定义按钮直接加人
 * @param {String} type 操作类型。区分是加签/当前会签/知会/多级会签
 * @param {Function} resolve
 * @return {Object}
 */
export async function getInsertNodeParams(people, type, resolve) {
  const result = getBasicParams(type);
  //如果是加签给某一个人
  if (people) {
    console.log('getInsertNodeParams', result, people);

    //如果直接是信息（处理页自定义按钮）,就直接合并
    if (typeof people === 'object') {
      Object.assign(result, people);
    } else {
      //新建页面
      await addNodeMessage.call(this, result, people);
      resolve && resolve();
    }
  }
  console.log('getInsertNodeParams', result);

  return result;
}
